package pers.qianyu.month_202102.date_20210217;

/**
 * 1312. 让字符串成为回文串的最少插入次数
 * https://leetcode-cn.com/problems/minimum-insertion-steps-to-make-a-string-palindrome/
 *
 * @author mizzle rain
 * @date 2021-02-17 11:01
 */
public class MinInsertions {
    public int minInsertions(String s) {
        char[] arr = s.toCharArray();
        int n = s.length();
        if (n == 0) return 0;
        int[][] dp = new int[n][n];
        for (int i = n - 2; i >= 0; i--) {
            for (int j = i + 1; j < n; j++) {
                if (arr[i] == arr[j]) dp[i][j] = dp[i + 1][j - 1];
                else dp[i][j] = Math.min(dp[i][j - 1], dp[i + 1][j]) + 1;
            }
        }
        return dp[0][n - 1];
    }
}
